/**
 * 去除字符串头尾空格或指定字符 
 * */ 
export function trim(str: string, c: string | RegExp | null) {
    if (c === null || c === "") {
        const _str = str.replace(/^s*/, '');
        const rg = /s/;
        let i = _str.length;
        while (rg.test(_str.charAt(--i)));
        return _str.slice(0, i + 1);
    }
    else {
        let rg = new RegExp("^" + c + "*");
        const _str = str.replace(rg, '');
        rg = new RegExp(c);
        let i = _str.length;
        while (rg.test(_str.charAt(--i)));
        return _str.slice(0, i + 1);
    }
}

/**
 * 去除字符串头部空格或指定字符 
 * */ 
export function trimStart(str: string, c: string | null) {
    if (c === null || c === "") {
        return str.replace(/^s*/, '');
    } else {
        const rg = new RegExp("^" + c + "*");
        return str.replace(rg, '');
    }
}

/**
 * 去除字符串尾部空格或指定字符 
 * */ 
export function trimEnd(str: string, c: string | RegExp | null) {
    let i = str.length;
    if (c === null || c === "") {
        const rg = /s/;
        while (rg.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    } else {
        const rg = new RegExp(c);
        while (rg.test(str.charAt(--i)));
    }
    if (c === '%') {
        return Number(str.slice(0, i + 1))
    }
    return str.slice(0, i + 1);
}